﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BestBuy.DAL;
namespace BestBuy.PageAdmin
{
    public partial class PageUsersManager : System.Web.UI.Page
    {
        private CThongTinCaNhan _cttcn = new CThongTinCaNhan();
        private static int _currentPage = 1;

        protected void Page_Load(object sender, EventArgs e)
        {
            pnError.Visible = false;
            pnEmtry.Visible = false;
            if (!IsPostBack)
            {
                setEnableCBB();
                Filter();
            }
        }

        public string CheckGioiTinh(object obj)
        {
            bool b = bool.Parse(obj.ToString());
            if (!b)
                return "Male";
            else
                return "Female";
        }

        private void Filter()
        {
            _currentPage = 1;
            string strQuery = UpdateFilterInfo();
            LoadTotalPageNumber(strQuery);
            LoadGV(strQuery);
        }

        protected void setCheckedFalseCHB()
        {
            chbCountry.Checked = false;
            chbTypeUserName.Checked = false;
            chbName.Checked = false;
        }
        protected void setEnableCBB()
        {
            cbbCountry.Enabled = false;
            cbbTypeUserName.Enabled = false;
            txtName.Enabled = false;
            
        }

        protected void XoaThongTinLoc()
        {
            CFilterInfoUser.Name = "";
            CFilterInfoUser.LoaiND = 0;
            CFilterInfoUser.IDCountry = 0;
            //CFilterInfoUser.CurrentPage = 1;
        }

        private void LoadGV(string strQuery)
        {
            txtTrangHienTai.Text = _currentPage.ToString();
            CFilterInfoUser.CurrentPage = _currentPage;
            int size = gridview.PageSize;
            int begin = (_currentPage - 1) * size;
            List<ThongTinTaiKhoan> list = _cttcn.GetList(strQuery,begin,size);
            if (list.Count > 0)
            {
                gridview.DataSource = list;
                gridview.DataBind();
                pnEmtry.Visible = false;
                lbGridViewEmtry.Text = "";
                pnPhanTrang.Visible = true;
            }
            else
            {
                pnEmtry.Visible = true;
                pnEmtry.CssClass = "InLeft info message";
                pnPhanTrang.Visible = false;
                gridview.DataSource = null;
                gridview.DataBind();
                lbGridViewEmtry.Text = "There are no data.";
                
            }
        }

        protected void LoadTotalPageNumber(string query)
        {
            int soLuongTrang = _cttcn.slTrangDuLieu(gridview.PageSize, query);
            if (soLuongTrang == 1) 
            {
                _currentPage = 1; 
            }
            lbTrang.Text = soLuongTrang.ToString();
        }

        protected string GetFilterInfo()
        {
            string where = "";
            setCheckedFalseCHB();
            setEnableCBB();
            if (!CFilterInfoUser.Name.Equals(""))
            {
                chbName.Checked = true;
                txtName.Enabled = true;
                txtName.Text = CFilterInfoUser.Name;
                where += " and dl.HoTen like N'%" + CFilterInfoUser.Name + "%'";
            }
            if (CFilterInfoUser.LoaiND > 0)
            {
                chbTypeUserName.Checked = true;
                cbbTypeUserName.Enabled = true;
                cbbTypeUserName.SelectedValue = CFilterInfoUser.LoaiND.ToString();
                where += " and dl.LoaiND=" + CFilterInfoUser.LoaiND.ToString();
            }
            if (CFilterInfoUser.IDCountry > 0)
            {
                chbCountry.Checked = true;
                cbbCountry.Enabled = true;
                cbbCountry.SelectedValue = CFilterInfoUser.IDCountry.ToString();
                where += " and dl.IDQuocGia=" + CFilterInfoUser.IDCountry.ToString();
            }
            return where;
        }

        protected string UpdateFilterInfo()
        {
            XoaThongTinLoc();
            string where = "";
            int ma;
            if (chbTypeUserName.Checked)
            {
                ma = int.Parse(cbbTypeUserName.SelectedValue.ToString());
                where += " and dl.LoaiND=" + ma.ToString();
                CFilterInfoUser.LoaiND = ma;
            }
            if (chbCountry.Checked)
            {
                ma = int.Parse(cbbCountry.SelectedValue.ToString());
                where += " and dl.IDQuocGia=" + ma.ToString();
                CFilterInfoUser.IDCountry = ma;
            }
            if (chbName.Checked && !txtName.Text.Equals(""))
            {
                where += " and dl.HoTen like N'%" + txtName.Text + "%'";
                CFilterInfoUser.Name = txtName.Text;
            }
            return where;
        }

        protected void lbtnBegin_Click(object sender, EventArgs e)
        {
            _currentPage = 1;
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void lbtnPre_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                string strQuery = GetFilterInfo();
                LoadGV(strQuery);
            }
        }

        protected void lbtnNext_Click(object sender, EventArgs e)
        {
            if (_currentPage < int.Parse(lbTrang.Text))
            {
                _currentPage++;
                string strQuery = GetFilterInfo();
                LoadGV(strQuery);
            }
        }

        protected void lbtnEnd_Click(object sender, EventArgs e)
        {
            _currentPage = int.Parse(lbTrang.Text);
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void lbtnToi_Click(object sender, EventArgs e)
        {
            int goToPage = Convert.ToInt32(txtTrangHienTai.Text);
            int soLuongTrang = int.Parse(lbTrang.Text);
            if (goToPage > soLuongTrang)
            {
                _currentPage = soLuongTrang;
            }
            else if (goToPage <= soLuongTrang)
            {
                _currentPage = goToPage;
            }
            string strQuery = GetFilterInfo();
            LoadGV(strQuery);
        }

        protected void lbtnXoa_Click(object sender, EventArgs e)
        {
            lbError.Text = "";
            int size = gridview.Rows.Count;
            bool kq = false;
            int idtaikhoan;
            string strquery = GetFilterInfo();
            for (int i = 0; i <= size - 1; i++)
            {
                try
                {
                    CheckBox pXoa = (CheckBox)gridview.Rows[i].FindControl("chkXoa");
                    if (pXoa.Checked)
                    {
                        idtaikhoan = Convert.ToInt32(((Label)gridview.Rows[i].FindControl("lbID")).Text);
                        kq = _cttcn.LockAccount(idtaikhoan);
                        if (!kq)
                        {
                            pnError.Visible = true;
                            pnError.CssClass = "InLeft error message";
                            lbError.Text = lbError.Text + "Error. The data that this code : "+ idtaikhoan.ToString() + " can not be locked.Please try again <br/>";
                        }
                    }
                }
                catch
                {
                    LoadTotalPageNumber(strquery);
                    LoadGV(strquery);
                }
            }
            if (kq)
            {
                pnError.Visible = true;
                pnError.CssClass = "InLeft success message";
                lbError.Text ="Lock account successfully";
            }
            LoadTotalPageNumber(strquery);
            LoadGV(strquery);
        }

        protected void chbName_CheckedChanged(object sender, EventArgs e)
        {
            if (chbName.Checked)
            {
                txtName.Enabled = true;
            }
            else
            {
                txtName.Enabled = false;
            }
        }

        protected void chbTypeUserName_CheckedChanged(object sender, EventArgs e)
        {
            if (chbTypeUserName.Checked)
            {
                cbbTypeUserName.Enabled = true;
            }
            else
            {
                cbbTypeUserName.Enabled = false;
            }
        }

        protected void chbCountry_CheckedChanged(object sender, EventArgs e)
        {
            if (chbCountry.Checked)
            {
                cbbCountry.Enabled = true;
            }
            else
            {
                cbbCountry.Enabled = false;
            }
        }

        protected void btnFilter_Click(object sender, EventArgs e)
        {
            Filter();
        }
    }
}